<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *住户管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'住户列表'),
	array('op'=>'edit','title'=>'添加住户'),
);
if($_RQ['op']=='list'){
	$params=array();
	$data=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$data['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=intval($_RQ['mobile']);
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if(isset($_RQ['shenfen'])){
		$params['shenfen']=intval($_RQ['shenfen']);
		$return['params']['shenfen']=$_RQ['shenfen'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','hid','mid','name','mobile','shenfen','status','createtime');
	$list=pdo_getslice('jy_smallquan_household',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$house=pdo_get('jy_smallquan_house',array('id'=>$item['hid']));
			$building=pdo_get('jy_smallquan_building',array('id'=>$house['bid']));
			$item['building']=$building;
			$house['dongshu']=$house['dongshu'].'栋';
			$house['danyuan']=$house['danyuan'].'单元';
			$item['house']=$house;
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'name','title'=>'姓名','type'=>'string'),
			array('name'=>'mobile','title'=>'手机号码','type'=>'string'),
			array('name'=>'shenfen','title'=>'身份','type'=>'radio','options'=>'shenfen'),
			array('name'=>'building.area','title'=>'区域','type'=>'string'),
			array('name'=>'building.qishu','title'=>'期数','type'=>'string'),
			array('name'=>'house.dongshu','title'=>'栋数','type'=>'string'),
			array('name'=>'house.danyuan','title'=>'单元','type'=>'string'),
			array('name'=>'house.room_num','title'=>'房号','type'=>'string'),
			array('name'=>'house.room_type','title'=>'房型','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
		)),
		'search'=>array(
			array('name'=>'name','width'=>15,'title'=>'姓名','type'=>'string'),
			array('name'=>'mobile','width'=>15,'title'=>'手机号码','type'=>'string'),
			array('name'=>'shenfen','width'=>15,'title'=>'身份','type'=>'select','options'=>'shenfen_select'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'select','options'=>'status_select'),
		)
	);
	$return['shenfen']=array('家庭成员','户主');
	$return['status']=array('待审核','已通过','不通过');
	$return['shenfen_select']=array(
		array('id'=>'0','title'=>'家庭成员'),
		array('id'=>'1','title'=>'户主'),	
	);
	$return['status_select']=array(
		array('id'=>'0','title'=>'待审核'),
		array('id'=>'1','title'=>'已通过'),	
		array('id'=>'2','title'=>'不通过'),	
	);
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_smallquan_household',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	$return['shenfen']=array('家庭成员','户主');
	$return['status']=array('待审核','已通过','不通过');
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回房屋列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'name','title'=>'姓名','type'=>'string'),
					array('name'=>'mobile','title'=>'手机号','type'=>'string'),
					array('name'=>'shenfen','title'=>'身份','type'=>'radio','options'=>'shenfen'),
					array('name'=>'status','title'=>'审核状态','type'=>'radio','options'=>'status'),
					array('type'=>'choose','title'=>'绑定会员id','name'=>'mid','action'=>'get_member','choose_title'=>'选择会员'),
					array('type'=>'choose','title'=>'绑定房屋id','name'=>'hid','action'=>'get_house','choose_title'=>'选择房屋'),
				)
			),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(empty($postdata['name'])){
		exi('请填写姓名','error');
	}
	if(empty($postdata['mobile'])){
		exi('请填写手机号','error');
	}
	if(!$postdata['mid']){
		exi('请绑定会员id','error');
	}
	if(!$postdata['hid']){
		exi('请绑定房屋id','error');
	}
	$data=array(
		'name'=>trim($postdata['name']),
		'mobile'=>trim($postdata['mobile']),
		'mid'=>intval($postdata['mid']),
		'hid'=>intval($postdata['hid']),
		'status'=>intval($postdata['status']),
		'shenfen'=>intval($postdata['shenfen']),
	);
	if($data['status']==1 && $data['hid']){
		pdo_update('jy_smallquan_house',array('status'=>1),array('id'=>$item['hid'],'uniacid'=>$_SESSION['uniacid']));
	}
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_smallquan_household',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_smallquan_household',$data);
	}
	exi('操作成功！','','list');
}

if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_smallquan_household',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}